package com.puboot.module.controller;

import com.puboot.component.Json2ClassConvert;
import com.puboot.module.model.LkCategory;
import com.puboot.module.model.LkChapter;
import com.puboot.module.model.LkComic;
import com.puboot.module.service.LkCategoryService;
import com.puboot.module.service.LkChapterService;
import com.puboot.module.service.LkComicService;
import com.puboot.module.vo.ImageDetVo;
import com.puboot.module.vo.ImageRecVo;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * (LkCategory)表控制层
 *
 * @author Leonidax
 * @since 2024-10-11 16:05:27
 */
@Controller
@AllArgsConstructor
@RequestMapping("comic")
public class ReadController {
    private final LkCategoryService categoryService;
    private final LkComicService comicService;
    private final LkChapterService chapterService;
    private String initCacheModel(Model model){
        //获取当前语言
        Locale locale = LocaleContextHolder.getLocale();
        String language = locale.getLanguage();
        //分类列表
        List<LkCategory> cates = categoryService.selectCategories();
        model.addAttribute("cates",cates);
        model.addAttribute("lang",language);
        return language;
    }

    @GetMapping("/login")
    public String login(){
        return "user/login";
    }
    @GetMapping("/register")
    public String register(){
        return "user/register";
    }

    @GetMapping("/{id}")
    public String comic(Model model, @PathVariable Integer id){
        String language = initCacheModel(model);
        LkComic comic = comicService.queryById(id,language);
        if(comic==null) return "redirect:/";
        model.addAttribute("comic",comic);
        List<LkChapter> list = chapterService.chapters(id, language);
        model.addAttribute("list",list);
        if(list.size()>0)
            model.addAttribute("firstId",list.get(list.size()-1).getId());
        else
            model.addAttribute("firstId",0);
        List<LkComic> random = comicService.random(id,3,language);
        model.addAttribute("random",random);
        return "index/comic";
    }

    @GetMapping("/more/{id}")
    public String more(Model model, @PathVariable Integer id){
        String language = initCacheModel(model);
        LkComic comic = comicService.queryById(id,language);
        model.addAttribute("comic",comic);
        List<LkChapter> list = chapterService.chapters(id, language);
        model.addAttribute("list",list);
        model.addAttribute("firstId",list.get(list.size()-1).getId());
        return "index/comic-more";
    }
    @GetMapping("/chapter/{comicId}/{id}")
    public String chapter(Model model, @PathVariable Integer comicId, @PathVariable Integer id){
        String language = initCacheModel(model);
        LkComic comic = comicService.queryById(comicId,language);
        model.addAttribute("comic",comic);
        LkChapter chapter = chapterService.queryById(id,language);
        List<String> imgs = Arrays.stream(chapter.getContent().split(",")).collect(Collectors.toList());
        model.addAttribute("imgs",imgs);
        model.addAttribute("chapter",chapter);
        HashMap<String,String> page = chapterService.getPageInfo(chapter,language);
        model.addAttribute("page",page);
        List<LkComic> random = comicService.random(comicId,3,language);
        model.addAttribute("random",random);
        if(chapter.getDialog() == null){
            model.addAttribute("detVos", null);
            model.addAttribute("recVos", null);
        }else {
            List<ImageDetVo> detVos = (List<ImageDetVo>) Json2ClassConvert.json2List(chapter.getPosition(), ImageDetVo.class);
            List<ImageRecVo> recVos = (List<ImageRecVo>) Json2ClassConvert.json2List(chapter.getDialog(), ImageRecVo.class);
            model.addAttribute("detVos", detVos);
            model.addAttribute("recVos", recVos);
        }
        return "index/chapter";
    }

}

